package com.my.all.service;

import com.my.all.mapper.GoodsMapper;
import com.my.all.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//将Service注解标记的类自动注册到SpringIoc容器中，由spring管理该类对象的生存周期
@Service
public class GoodsService {
    //注入某个对象到SpringIoc容器中，从而自动给该对象赋值
    @Autowired
    GoodsMapper goodsMapper;

    public int insertGoods(String goodsName, String goodsCount, String goodsPrice, String goodsInfo){

        Goods goods = new Goods(null,goodsName,Integer.parseInt(goodsCount),
                Double.parseDouble(goodsPrice),goodsInfo);
        int result = goodsMapper.insertGoods(goods);
        return result;
    }

    public int deleteGoodsById(Long id){
        return goodsMapper.deleteGoodsById(id);
    }

    public List<Goods> selectAll(){
        return goodsMapper.selectAll();
    }

    public Goods selectGoodsById(String goodsId){
        return goodsMapper.selectGoodsById(Long.parseLong(goodsId));

    }

    //更新商品
    public Goods updateGoods(String goodsId, String goodsCount, String goodsPrice, String goodsInfo){
        Goods newGoods = null;
        Goods oldGoods = selectGoodsById(goodsId);
        if((goodsPrice == null || goodsPrice.equals(""))&&(goodsInfo == null || goodsInfo.equals(""))){
            //只更新数量--1
            newGoods = new Goods(Long.parseLong(goodsId), null, Integer.parseInt(goodsCount),
                    oldGoods.getGoodsPrice(),oldGoods.getGoodsInfo());
        } else if((goodsCount == null || goodsCount.equals(""))&&(goodsInfo == null || goodsInfo.equals(""))){
            //只更新价格--1
            newGoods = new Goods(Long.parseLong(goodsId), null, oldGoods.getGoodsCount(),
                    Double.parseDouble(goodsPrice),oldGoods.getGoodsInfo());
        } else if((goodsPrice == null || goodsPrice.equals(""))&&(goodsCount == null || goodsCount.equals(""))){
            //只更新信息--1
            newGoods = new Goods(Long.parseLong(goodsId), null, oldGoods.getGoodsCount(),
                    oldGoods.getGoodsPrice(),goodsInfo);
        } else if((goodsInfo.equals("")) || goodsInfo == null){
            //只更新数量和价格--1
            newGoods = new Goods(Long.parseLong(goodsId), null, Integer.parseInt(goodsCount),
                    Double.parseDouble(goodsPrice),oldGoods.getGoodsInfo());
        } else if((goodsPrice == null || goodsPrice.equals(""))){
            //只更新数量和信息--1
            newGoods = new Goods(Long.parseLong(goodsId), null, Integer.parseInt(goodsCount),
                    oldGoods.getGoodsPrice(), goodsInfo);
        } else if((goodsCount == null || goodsCount.equals(""))){
            //只更新价格和信息--1
            newGoods = new Goods(Long.parseLong(goodsId), null, oldGoods.getGoodsCount(),
                    Double.parseDouble(goodsPrice), goodsInfo);
        } else{
            //更新数量，价格和信息--1
            newGoods = new Goods(Long.parseLong(goodsId), null, Integer.parseInt(goodsCount),
                    Double.parseDouble(goodsPrice), goodsInfo);
        }

        if(goodsMapper.updateGoods(newGoods)==1){  //更新成功
            return newGoods;
        } else {  //更新失败
            return null;
        }
    }

}
